package com.aplink.generic.widgets;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;

import com.aplink.generic.GenericFragment;
import com.aplink.generic.adapter.GenericConfigAdapter;
import com.aplink.generic.constant.BundleConstant;
import com.aplink.generic.constant.ConfigParameter;
import com.aplink.generic.constant.SqlDataType;
import com.aplink.generic.database.SqlMethods;
import com.aplink.generic.pojo.GenericAttribute;
import com.aplink.generic.pojo.GenericObject;
import com.aplink.generic.pojo.ObjPropCol;
import com.aplink.generic.pojo.ObjectFilter;
import com.aplink.generic.pojo.controlspec.ControlSpec;
import com.aplink.generic.pojo.controlspec.GridViewSpec;
import com.aplink.generic.pojo.xml.ColumnSetting;
import com.aplink.generic.util.LogUtil;
import com.aplink.generic.util.convertcount.GenericFilter;
import com.aplink.generic.util.objectrule.TargetRuleCollection;
import com.aplink.generic.xmlparser.ColumnSettingParser;
import com.aplink.generic.xmlparser.ObjPropInfoParser;

import java.util.ArrayList;

public class CustomGridView extends BaseView {
    private GridView mGridView;
    private GenericConfigAdapter mGenericConfigAdapter;
    private TargetRuleCollection targetCollection;
    private GridViewSpec mGridViewSpecFormat;
    private LogUtil mLogUtil = new LogUtil( getClass() );
    private ArrayList<ColumnSetting> columnSettings;
    private ArrayList<GenericObject> mGenericObjects;

    public CustomGridView( GenericFragment genericFragment,
                           ControlSpec controlSpec, View view ) {
        super( genericFragment, controlSpec, view );
        mGridView = (GridView) view;
        mGridViewSpecFormat = (GridViewSpec) getControlSpec();
        targetCollection = mGridViewSpecFormat.getTargetRuleCollection();
    }

    @Override
    protected void loadDataToView() {

    }

    @Override
    protected void doIfNotHashPassedValues() {

    }

    @Override
    protected void setEventListener() {
        this.mGridView.setOnItemClickListener( mOnRowClickListener );
    }

    private GenericConfigAdapter generateConfigAdapter() {
        this.columnSettings = ColumnSettingParser.parserIntoColumnSetting(
                getApplicationContext(), mGridViewSpecFormat.getIdRptrDesign(),
                ConfigParameter.COL_TAGNAME_COLUMN );
        return new GenericConfigAdapter( getApplicationContext(),
                this.mGenericObjects, this.columnSettings,
                mGridViewSpecFormat.getRowItem() );
    }

    @Override
    protected void initViewProperties() {
        ObjectFilter filter = mGridViewSpecFormat.getFilter();

        this.mGenericObjects = this.loadData( filter );
        if ( mGenericObjects != null ) {
            this.mGenericConfigAdapter = this.generateConfigAdapter();
            this.mGridView.setAdapter( this.mGenericConfigAdapter );
        }
    }

    @Override
    public GenericAttribute<?, ?> getAttribute() {
        return null;
    }

    private OnItemClickListener mOnRowClickListener = new OnItemClickListener() {

        @Override
        public void onItemClick( AdapterView<?> arg0, View arg1, int index,
                                 long arg3 ) {
            if ( targetCollection != null ) {
                String fullClassName = null;
                try {
                    GenericObject genericObject = mGenericConfigAdapter
                            .getDisplayResult().get( index );

                    fullClassName = GenericFilter.getTargetActivityFromRule(
                            targetCollection, genericObject );
                    if ( fullClassName == null ) {
                        fullClassName = targetCollection.getDefaultTarget();
                    }
                    Class<?> targetActivity = Class.forName( fullClassName );
                    Intent intent = new Intent( getApplicationContext(),
                            targetActivity );
                    Bundle bundle = new Bundle();
                    bundle.putSerializable( BundleConstant.LIST_OBJECT,
                            mGenericObjects );
                    bundle.putInt( BundleConstant.SELECTED_INDEX, index );
                    bundle.putString( BundleConstant.PARENT,
                            getDataEntryActivity().getClassName() );
                    intent.putExtras( bundle );
                    getActivity().startActivity( intent );
                } catch ( ClassNotFoundException e ) {
                    mLogUtil.error( fullClassName + " is not found",
                            "Grid view item click", e );
                }
            }
        }
    };

    private ArrayList<GenericObject> loadData( ObjectFilter filter ) {
        ObjPropCol objPropCol = ObjPropInfoParser.parseIntoObjProp(
                getApplicationContext(),
                mGridViewSpecFormat.getIdContentData(),
                ConfigParameter.OBJ_PROP_TAGNAME,
                mGridViewSpecFormat.getDataObject() );

        if ( mFilterDataType == null ) {
            mFilterDataType = objPropCol.getByKey( filter.getMappingName() )
                    .getDataType();
        }

        if ( ( filter != null ) && ( filter.getFilterValue() != null )
                && ! filter.isShowAll() ) {
            String whereClause = SqlMethods.getInstance().equal( mFilterDataType,
                    filter.getDataObject() + "." + filter.getMappingName(),
                    filter.getFilterValue() ).toString();
            return getSelectCommand().getList( objPropCol,
                    whereClause );
        } else {
            return getSelectCommand().getList( objPropCol, null );
        }
    }

    private SqlDataType mFilterDataType;

    public void filter( String filterValue ) {
        ObjectFilter filter = mGridViewSpecFormat.getFilter();
        filter.setFilterValue( filterValue );
        mGenericObjects = loadData( filter );
        setAdapter( mGenericObjects );
    }

    private void setAdapter( ArrayList<GenericObject> genericObjects ) {
        mGenericConfigAdapter.setDisplayResult( genericObjects );
        mGenericConfigAdapter.notifyDataSetChanged();
    }
}
